package com.application.Activity;

import java.util.ArrayList;

import ReadWriteXML.COption;
import ReadWriteXML.CQuestion;
import ReadWriteXML.CUsingQuestionList;
import ReadWriteXML.ReadXML;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;

import com.application.Adapter.ViewExerciseAdapter;
import com.application.Adapter.ViewExerciseAdapter.ViewExercise;
import com.application.Controller.ActivityController;
import com.application.Controller.ReadWriteFileController;

public class FrmViewExercise extends Activity{
	CUsingQuestionList _uqlist;
	private static int _idPart = 0;
	private static int _idDetailLearning= 0;
	private static int _idExericise = 0;
	private String path = "";

	static ListView listExercise;
	TextView tvTitle;	
	private static ArrayList<ViewExercise> _viewExerciseList = new ArrayList<ViewExerciseAdapter.ViewExercise>();
	private static ViewExerciseAdapter _arrayAdapter;
	private static Context _cont;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frm_view_exercise);
		Log.d("PEERDROID","vao FrmViewExercise");
		Log.d("PEERDROID","vao _idPart: " + _idPart);
		Log.d("PEERDROID","vao _idDetail: " + _idDetailLearning);
		Log.d("PEERDROID","vao _idex: " + _idExericise);
		_cont = this;
		listExercise = (ListView)findViewById(R.id.frmViewExercise_List);
		tvTitle = (TextView)findViewById(R.id.frmviewexercise_tvTitleBanner);
		path = ReadWriteFileController.DIRECTORYTESTCONTENT + _idPart + "_" + get_idExericise() + "_GroupTest_" + get_idDetailLearning()+".xml";
		Log.d("PEERDROID","path " + path);
		showList();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d("PEERDROID","vao onstart list test");
		ActivityController.setPeerActivity(this);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//		ShowSubjectList();	
		Log.d("PEERDROID", "vao resume");
		ActivityController.setPeerActivity(this);
	}
	private void showList(){
		_uqlist = ReadXML.ReadExerciseGroup(path);
		_viewExerciseList.clear();
		Log.d("PEERDROID","_uqlist: " + _uqlist.get_questionList().size());
		int i = 1;
		ArrayList<CQuestion> quesList = _uqlist.get_questionList();
		for(CQuestion ques : quesList)
		{
			int type = ques.get_type();
			String content = ques.get_content();
			switch (type) {
			case 1://single choice
			{
				content = "Câu "+ (i++) +" :"+ content ; 
				content += "(Chọn 1 đáp án)";
				break;
			}
			case 2://multi choice
			{
				content = "Câu "+ (i++) +" :"+ content ; 
				content += "(Chọn nhiều đáp án)";
				break;
			}
			case 3://true/flase
			{
				content = "Câu "+ (i++) +" :"+ content ; 
				content += "(Câu hỏi đúng sai)";
				break;
			}
			case 5://writequestion
			{
				content = "Câu "+ (i++) +" :"+ content ; 
				content += "(Câu hỏi tự luận)";
				break;
			}
			default:
				break;
			}
			String option = "";
			int j = 0;
			String[] temp = {"a/ ", "b/ ", "c/ ", "d/ ", "e/ ", "f/ ", "g/ ", "h/ "};
			if(type!=5)
			{
				for(COption coption: ques.get_listOption())
				{
					option += temp[j++] + coption.get_content() + "\n";
				}
			}
			ViewExercise v = new ViewExercise(type, content, option);
			_viewExerciseList.add(v);
		}
		setListAdapter();
	}	
	public static void setListAdapter() {

		_arrayAdapter = new ViewExerciseAdapter(_cont,R.layout.row_viewexercise,_viewExerciseList);
		listExercise.setAdapter(_arrayAdapter);

	}
	public static void setIdPart(int idPart) {
		FrmViewExercise._idPart = idPart;
	}
	public static int getIdPart() {
		return _idPart;
	}


	public static void set_idDetailLearning(int _idDetailLearning) {
		FrmViewExercise._idDetailLearning = _idDetailLearning;
	}


	public static int get_idDetailLearning() {
		return _idDetailLearning;
	}


	public static void set_idExericise(int _idExericise) {
		FrmViewExercise._idExericise = _idExericise;
	}


	public static int get_idExericise() {
		return _idExericise;
	}

}
